class ListNode:
    def __init__(self, x, y):
        self.val = x
        self.next = y


# class Solution(object):
#     def isPalindrome(self, head:ListNode) -> bool:
#         """
#         :type head: ListNode
#         :rtype: bool
#         """
#         p, q = head, None
#         while p:
#             q ,p = ListNode(p.val, q), p.next
#         while q and q.val==head.val:
#             q, head = q.next, head.next
#         return q is None

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        p, q = head, None
        while p:
            q, p = ListNode(p.val, q), p.next
        while q and q.val == head.val:
            q, head = q.next, head.next
        return q is None
